﻿#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>
using namespace std;

//地图宽度
#define W 10
//地图高度
#define H 10

void drewMap(int map[H][W]);//绘制地图
void move(int map[H][W]);//玩家移动
int win(int map[H][W]);//是否通关判断
void switchMap(int map[H][W], int level);//切换游戏地图
void ing(int step, int level);//绘制信息界面
void gotoxy(int x, int y);//移动光标

//地图数组
//0空地 1墙壁 2箱子 3玩家 4目的地
//第一关
int map1[H][W] = {
		{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,1,1,1,0,0,0,0},
		{0,0,0,1,4,1,0,0,0,0},
		{0,0,0,1,0,1,1,1,1,0},
		{0,1,1,1,2,2,0,4,1,0},
		{0,1,4,2,3,0,1,1,1,0},
		{0,1,1,1,1,2,1,0,0,0},
		{0,0,0,0,1,4,1,0,0,0},
		{0,0,0,0,1,1,1,0,0,0},
		{0,0,0,0,0,0,0,0,0,0}
};
//第二关
int map2[H][W] = {
	{0,0,0,0,0,0,0,0,0,0},
	{0,1,1,1,1,1,1,1,1,0},
	{0,1,4,2,0,0,2,4,1,0},
	{0,1,0,1,4,0,1,0,1,0},
	{0,1,0,1,2,0,1,0,1,0},
	{0,1,4,2,3,0,0,0,1,0},
	{0,1,0,1,0,0,1,0,1,0},
	{0,1,4,2,0,0,2,4,1,0},
	{0,1,1,1,1,1,1,1,1,0},
	{0,0,0,0,0,0,0,0,0,0}
};
//第三关
int map3[H][W] = {
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0},
	{0,1,1,1,1,1,1,1,0,0},
	{0,1,4,0,0,0,0,1,1,0},
	{0,1,4,1,0,2,2,0,1,0},
	{0,1,4,4,2,2,0,0,1,0},
	{0,1,0,4,0,0,2,3,1,0},
	{0,1,1,1,1,1,1,1,1,0},
	{0,0,0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0,0,0}
};

int main() {

	int level = 1; //关卡初始化
	int index;
	
	int map[H][W] = { 0 };//初始化地图    
	while (true) {
		int step = 0;
		while (true) {
			if (step == 0) {
				switchMap(map, level);
			}
			drewMap(map);
			ing(step, level);
			move(map);
			
			step++;
			system("cls");
			index = win(map);
			if (index == -1) {
				break;
			}
		}
		if (level == 3) {
			break;
		}
		level++;
	}
	cout << "欢迎此次游玩!";
	system("cls");
}

void drewMap(int map[H][W]) {
	for (int i = 0; i < W; i++) {
		for (int j = 0; j < H; j++) {
			switch (map[i][j])
			{
			case 0:
				printf("  ");
				break;
			case 1:
				printf("# ");
				break;
			case 2:
				printf("& ");
				break;
			case 3:
				printf("$ ");
				break;
			case 4:
				printf("X ");
				break;
			case 6:
				printf("& ");
				break;
			case 7:
				printf("$ ");
				break;
			}
		}
		printf("\n");
	}
}

void move(int map[H][W]) {
	//获取玩家坐标
	int i, j;
	for (i = 0; i < W; i++) {
		for (j = 0; j < H; j++) {
			if (map[i][j] == 3) {
				goto AAA;
			}
		}
	}
AAA:
	gotoxy(30, 2);
	cout << "* * * * * * * * * * * * * *";
	gotoxy(30, 3);
	cout << "*    坐标 :(" << i << "," << j << ")\t\t*";
	gotoxy(0, 0);
	switch (_getch())
	{
	case 'w':
		if (map[i - 1][j] == 0 || (map[i - 1][j] == 2 && map[i - 2][j] == 0) || (map[i - 1][j] == 2 && map[i - 2][j] == 4) || map[i - 1][j] == 4) {
			if (map[i - 1][j] == 2) {
				map[i - 2][j] += 2;
				}
				map[i - 1][j] = 3;
				map[i][j] = 0;
			}
		break;
	case 'a':
		if (map[i][j - 1] == 0 || (map[i][j - 1] == 2 && map[i][j - 2] == 0) || (map[i][j - 1] == 2 && map[i][j - 2] == 4) || map[i][j - 1] == 4) {
			if (map[i][j - 1] == 2) {
				map[i][j - 2] += 2;
			}
			map[i][j - 1] = 3;
			map[i][j] = 0;
		}
		break;
	case 's':
		if (map[i + 1][j] == 0 || (map[i + 1][j] == 2 && map[i + 2][j] == 0) || (map[i + 1][j] == 2 && map[i + 2][j] == 4) || map[i + 1][j] == 4) {
			if (map[i + 1][j] == 2) {
				map[i + 2][j] += 2;
			}
			map[i + 1][j] = 3;
			map[i][j] = 0;
		}
		break;
	case 'd':
		if (map[i][j + 1] == 0 || (map[i][j + 1] == 2 && map[i][j + 2] == 0) || (map[i][j + 1] == 2 && map[i][j + 2] == 4) || map[i][j + 1] == 4) {
			if (map[i][j + 1] == 2) {
				map[i][j + 2] += 2;
			}
			map[i][j + 1] = 3;
			map[i][j] = 0;
		}
		break;
	}
}

int win(int map[H][W]) {
	int i, j;
	for (i = 0; i < 10; i++) {
		for (j = 0; j < 10; j++) {
			if (map[i][j] == 4) {
				return 1;
			}
		}
	}
	return -1;
}

void switchMap(int map[H][W],int level) {
	int i, j;
	switch (level){
		case 1:
			for (i = 0; i < H; i++) {
				for (j = 0; j < W; j++) {
					map[i][j] = map1[i][j];
				}
			}
			break;
		case 2:
			for (i = 0; i < H; i++) {
				for (j = 0; j < W; j++) {
					map[i][j] = map2[i][j];
				}
			}
			break;
		case 3:
			for (i = 0; i < H; i++) {
				for (j = 0; j < W; j++) {
					map[i][j] = map3[i][j];
				}
			}
			break;
		default:
			break;
	}
}
void ing(int step, int level) {
	gotoxy(30, 5);
	cout << "*    步数:" << step << "\t\t*";
	gotoxy(30, 7);
	cout << "*    当前关卡: 第" << level << "关\t*";
	gotoxy(30, 8);
	cout << "* * * * * * * * * * * * * *";

}

void gotoxy(int x, int y) //5.光标移动
{
	//调用win32 API去设置控制台的光标位置
	 //1.找到控制台的这个窗口
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	//2.光标的结构体
	COORD coord;
	//3.设置光标
	coord.X = x;
	coord.Y = y;
	//4.同步到控制台  Set Console Cursor Position
	SetConsoleCursorPosition(handle, coord);
}